package com.dfbz.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter({"/index.jsp","/forget.jsp","/register.jsp"})
public class SignFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie:cookies) {
            if ("remeberMe".equalsIgnoreCase(cookie.getName())){
                resp.sendRedirect(req.getContextPath()+"/assets/html/home.jsp");
                return;
            }
        }
        filterChain.doFilter(req,resp);
    }

    @Override
    public void destroy() {
    }
}
